from django.db import models
from django.contrib.auth.hashers import make_password

# Create your models here.
class Type(models.Model):  # 对话类型
    name = models.CharField(max_length=20)  # 类型名称


class Tu(models.Model):  # 图片
    tu_url = models.CharField(max_length=100)  # 图片的地址
    title = models.CharField(max_length=10)  # 名称
    content = models.CharField(max_length=600)  # 介绍
    xing = models.CharField(max_length=50)  # 星座
    guan = models.CharField(max_length=100)  # 关键字


class YongHu(models.Model):
    CHOICES = (('1', '普通用户'), ('2', '管理员'))

    name = models.CharField(max_length=20, unique=True, verbose_name="用户名")
    pwd = models.CharField(max_length=128, verbose_name="加密密码")  # 扩展密码存储长度
    key = models.CharField(max_length=10, choices=CHOICES, default='2', verbose_name="权限等级")

    def set_password(self, raw_password):
        """密码加密方法"""
        self.pwd = make_password(raw_password)
        self.save()

    @classmethod
    def create_admin(cls, name, password):
        """创建管理员专用方法"""
        user = cls(name=name, key='2')
        user.set_password(password)
        return user


class WenDa(models.Model):
    name = models.CharField(max_length=20)  # 对话的名称
    ai_array = models.JSONField(blank=True, null=True)  # ai的回答,数组
    hu_array = models.JSONField(blank=True, null=True)  # 用户的回答,数组
    tu_array = models.JSONField(blank=True, null=True)  # 图片,数组,用户可能选多个
    created_time = models.DateTimeField(auto_now_add=True)  # 对话创建的时间
    yonghu = models.ForeignKey(to=YongHu, on_delete=models.CASCADE)
    type = models.ForeignKey(to=Type, on_delete=models.CASCADE)
    tu = models.ForeignKey(to=Tu, on_delete=models.CASCADE)


class DaPing(models.Model):
    name = models.CharField(max_length=30)  # 数据的名称 就业
    hang_array = models.JSONField(blank=True, null=True)  # 行业的名称
    shu_array = models.JSONField(blank=True, null=True)  # 行业的数据
# 图片的上传
class UploadedImage(models.Model):
    image = models.ImageField(upload_to='uploads/%Y/%m/%d/')  # 按日期分目录存储
    created_at = models.DateTimeField(auto_now_add=True,null=True)

class Article(models.Model):
    title = models.CharField(max_length=100)#评论者
    content = models.TextField()#内容
    create_time = models.DateTimeField(auto_now_add=True)  # 评论创建时间

class Comment(models.Model):
    myname=models.CharField(max_length=30)#评论的用户名称
    content = models.TextField()  # 评论内容
    create_time = models.DateTimeField(auto_now_add=True)  # 评论创建时间
    user = models.ForeignKey(to=YongHu, on_delete=models.CASCADE)  # 关联用户，用户删除则评论也删除
    article = models.ForeignKey(to=Article, on_delete=models.CASCADE)  # 关联被评论对象（文章），文章删除则评论删除
    parent_comment = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)  # 自关联，用于回复评论，指向父评论

#牌组的介绍
class Paizu(models.Model):
    zuname=models.CharField(max_length=30)#牌组的名称
    content=models.TextField()#牌组的内容
    guan=models.CharField(max_length=30)#排族的关键词介绍
    paitu_array = models.JSONField(blank=True, null=True)  # 图片,数组,可能选多个
#活动的创建等
class Activity(models.Model):
    activity_name = models.CharField(max_length=100)  # 活动名称
    activity_description = models.TextField()  # 活动描述
    start_time = models.DateTimeField()  # 活动开始时间
    end_time = models.DateTimeField()  # 活动结束时间
    participants = models.ManyToManyField(YongHu, related_name='participated_activities')  # 多对多关系，关联用户表

class Background(models.Model):
    name=models.CharField(max_length=30)#背景图名称
    back_url=models.CharField(max_length=100)#背景图路径
    content=models.CharField(max_length=100)#背景图的描述

